/**
 * 封装http请求
 */
import config from './config.js'
const http = (ops) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: ops.method || 'post',
			url: `${config.baseUrl}${ops.url}`,
			data: ops.data,
			header: {
				// 'token': 'f5c6/3H2DlOCW8QtljhPJnGguanII2cLGRNaYfnk' //自定义请求头信息
			},
			success: (res) => {
				console.log(res)
				res = res.data
				if (res.code == 0 || res.code == 1) {
					resolve(res)
				} else {
					// 某些异常code 需要回到页面
					if (res.code == 95 || res.code == 97 || res.code == 99) {		
						console.log(res.code)
						console.log('登录过期状态或其他异常情况');
						resolve(res)
						return
					} else if(res.code == 404) {
						console.log('跳转404页面')
						uni.redirectTo({
							url:'/pages/packageB/error/error'
						})
						return
					}else if(res.code == 98 || res.code == 96) {
						uni.redirectTo({
							url:'/pages/packageA/login/login'
						})
						uni.showToast({
							title: res.message+'，请先登录',
							icon: 'none',
							duration: 1500
						})
						return
					}
					// 统一处理弹出异常code情况
					uni.showToast({
						title: res.message,
						icon: 'none',
						duration: 1500
					})
				}
			},
			fail: (err) => {
				// console.log(err);
				let data = err.data
				reject(data)
				let code = data.code
				switch (code) {
					case 1:
						// do sth
						break;
				}
			},
			complete: () => {
				setTimeout(() => {
					uni.hideLoading()
				}, 300)
			}
		})
	})
}

export default http
